package jsyf.oa.service.report;

import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.crm.ExhibitionCustomerInfoDao;
import jsyf.oa.dao.crm.ExhibitionDao;
import jsyf.oa.dao.report.ExhibitionReportDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.entity.crm.ExhibitionCusInfo;
import jsyf.oa.facade.report.ExhibitionReportFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/7 0007.
 */
@SuppressWarnings("all")
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class ExhibitionReportService implements ExhibitionReportFacade {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ExhibitionDao exhibitionDao;
    @Autowired
    private ExhibitionCustomerInfoDao exhibitionCustomerInfoDao;
    @Autowired
    private ExhibitionReportDao exhibitionReportDao;

    @Override
    public JSONObject getExhibitionByCard(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dataTime = getYesterday();
        JSONObject exhibitionCard = exhibitionReportDao.getExhibitionByCard(dataTime, companyId);
        return exhibitionCard;
    }

    @Override
    public List<JSONObject> getExhibitionByCompany() {
        //JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dataTime = getYesterday();
        return exhibitionReportDao.getExhibitionByCompany(dataTime);
    }

    @Override
    public List<JSONObject> getExhibitionList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        StringBuilder condition = new StringBuilder();
        condition.append(" AND companyId= '" + companyId + "'");
        condition.append(" ORDER BY DATE_FORMAT(exhibitionDate,'%Y-%m-%d')");
        return  exhibitionReportDao.getExhibitionList(condition.toString());
    }

    @Override
    public List<JSONObject> getExhibitionByRegion(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String exhibitionId = jsonParams.getString("exhibitionId");
        String regionId = jsonParams.getString("regionId");
        Map<String, JSONObject> map = exhibitionDao.getRegion2ManagerMap();
        Map<String, ExhibitionCusInfo> exhibitionCustomMap = getExhibitionCustomMap(exhibitionId);
        Map<String, JSONObject> mapAims = exhibitionDao.getExhibitionAimsMap(exhibitionId);
        List<JSONObject> regionList = exhibitionReportDao.getRegionListByRegionId(regionId);
        JSONObject objectTemp;
        for (int i = 0; i < regionList.size(); i++) {
            String regionName = regionList.get(i).get("regionName").toString();
            String bigRegionName = regionList.get(i).get("bigRegionName").toString();
            if (regionName.contains("大区")) {
                regionList.get(i).put("bigRegionName", regionName.replace("大区", ""));
            } else {
                regionList.get(i).put("bigRegionName", bigRegionName);
            }

            String regionIds = regionList.get(i).get("regionId").toString();
            if (map.containsKey(regionIds)) {
                regionList.get(i).put("userName", map.get(regionIds).get("USERNAME"));
                regionList.get(i).put("userId", map.get(regionIds).get("USERID"));
            } else {
                regionList.get(i).put("userName", "");
                regionList.get(i).put("userId", "");
            }

            if (mapAims.containsKey(regionIds)) {
                regionList.get(i).put("customNum", mapAims.get(regionIds).get("customNum"));
                regionList.get(i).put("depositNum", mapAims.get(regionIds).get("depositNum"));
            } else {
                regionList.get(i).put("customNum", "0");
                regionList.get(i).put("depositNum", "0");
            }
            if (exhibitionCustomMap.containsKey(regionIds)) {
                regionList.get(i).put("deposit1", exhibitionCustomMap.get(regionIds).getDeposit1());
                regionList.get(i).put("deposit2", exhibitionCustomMap.get(regionIds).getDeposit2());
                regionList.get(i).put("deposit3", exhibitionCustomMap.get(regionIds).getDeposit3());
                regionList.get(i).put("deposit4", exhibitionCustomMap.get(regionIds).getDeposit4());
                regionList.get(i).put("participationNum", exhibitionCustomMap.get(regionIds).getParticipationNum());
            } else {
                regionList.get(i).put("deposit1", 0);
                regionList.get(i).put("deposit2", 0);
                regionList.get(i).put("deposit3", 0);
                regionList.get(i).put("deposit4", 0);
                regionList.get(i).put("participationNum", 0);
            }

            for (int j = i + 1; j < regionList.size(); j++) {
                if (regionList.get(i).get("regionId").equals(regionList.get(j).get("parentId"))) {
                    objectTemp = regionList.get(j);
                    regionList.remove(j);
                    regionList.add(i + 1, objectTemp);
                }
            }
        }

        ExhibitionCusInfo exhibitionCusInfo = new ExhibitionCusInfo();
        Integer bigAreaIndex = 0;
        for (int i = 0; i < regionList.size(); i++) {
            Integer participationNum = Integer.parseInt(regionList.get(i).get("participationNum").toString());
            Integer deposit1 = Integer.parseInt(regionList.get(i).get("deposit1").toString());
            Integer deposit2 = Integer.parseInt(regionList.get(i).get("deposit2").toString());
            Integer deposit3 = Integer.parseInt(regionList.get(i).get("deposit3").toString());
            Integer deposit4 = Integer.parseInt(regionList.get(i).get("deposit4").toString());
            exhibitionCusInfo.setParticipationNum(exhibitionCusInfo.getParticipationNum() + participationNum);
            exhibitionCusInfo.setDeposit1(exhibitionCusInfo.getDeposit1() + deposit1);
            exhibitionCusInfo.setDeposit2(exhibitionCusInfo.getDeposit2() + deposit2);
            exhibitionCusInfo.setDeposit3(exhibitionCusInfo.getDeposit3() + deposit3);
            exhibitionCusInfo.setDeposit4(exhibitionCusInfo.getDeposit4() + deposit4);

            String parentId = regionList.get(i).get("parentId").toString();
            if (parentId.equals("0") && i > 0 || i == regionList.size() - 1) {
                regionList.get(bigAreaIndex).put("participationNum", exhibitionCusInfo.getParticipationNum());
                regionList.get(bigAreaIndex).put("deposit1", exhibitionCusInfo.getDeposit1());
                regionList.get(bigAreaIndex).put("deposit2", exhibitionCusInfo.getDeposit2());
                regionList.get(bigAreaIndex).put("deposit3", exhibitionCusInfo.getDeposit3());
                regionList.get(bigAreaIndex).put("deposit4", exhibitionCusInfo.getDeposit4());
                bigAreaIndex = i;
                exhibitionCusInfo = new ExhibitionCusInfo();
                continue;
            }
        }

        return regionList;
    }

    @Override
    public List<JSONObject> getExhibitionCustomer(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String exhibitionId = jsonParams.getString("exhibitionId");
        List<JSONObject> jsonObjectList = exhibitionReportDao.getExhibitionCustomerInfo(userId, exhibitionId);
        Map<String, JSONObject> jsonObjectMap = exhibitionCustomerInfoDao.getExhibitionIntentionMap(exhibitionId);
        Map<String, JSONObject> rsMap = exhibitionCustomerInfoDao.getRegion2Support();
        Map<String, JSONObject> trackMap = exhibitionCustomerInfoDao.getTrackMap(exhibitionId);
        for (JSONObject jsonObject : jsonObjectList) {
            String id = jsonObject.get("ID").toString();
//            String trackTime = jsonObject.get("TRACK_TIME").toString();
            String arrivalsTime = jsonObject.get("ARRIVALS_TIME").toString();
            String depositStatus = jsonObject.get("DEPOSIT_STATUS").toString();
            String travelModel = jsonObject.get("TRAVEL_MODEL").toString();
            String trackStatus = jsonObject.get("TRACK_STATUS").toString();
            String regionId = jsonObject.get("regionId").toString();
            String customerId = jsonObject.get("CUSTOMER_ID").toString();

            if (depositStatus.equals("1"))
                jsonObject.put("depositStatusName", "已交");
            else if (depositStatus.equals("2"))
                jsonObject.put("depositStatusName", "肯定交");
            else if (depositStatus.equals("3"))
                jsonObject.put("depositStatusName", "不确定");
            else if (depositStatus.equals("4"))
                jsonObject.put("depositStatusName", "不交");
            else
                jsonObject.put("depositStatusName", "");

            if (travelModel.equals("1"))
                jsonObject.put("travelModelName", "飞机");
            else if (travelModel.equals("2"))
                jsonObject.put("travelModelName", "火车");
            else if (travelModel.equals("3"))
                jsonObject.put("travelModelName", "自驾");
            else
                jsonObject.put("travelModelName", "");

            if (trackStatus.equals("1"))
                jsonObject.put("trackStatusName", "跟踪中");
            else if (trackStatus.equals("2"))
                jsonObject.put("trackStatusName", "放弃");
            else
                jsonObject.put("trackStatusName", "");

//            jsonObject.put("TRACK_TIME", DateUtils.parse(trackTime, "MM/dd"));
            jsonObject.put("ARRIVALS_TIME", DateUtils.parse(arrivalsTime, "MM/dd"));

            if (jsonObjectMap.containsKey(id)) {
                JSONObject object = jsonObjectMap.get(id);

                jsonObject.put("modelName", object.get("modelName"));
                jsonObject.put("quote", object.get("QUOTE"));
                jsonObject.put("acceptPrice", object.get("ACCEPT_PRICE"));
            } else {
                jsonObject.put("modelName", "");
                jsonObject.put("quote", "");
                jsonObject.put("acceptPrice", "");
            }

            if (rsMap.containsKey(regionId))
                jsonObject.put("supportName", rsMap.get(regionId).get("userName"));
            else
                jsonObject.put("supportName", "");

            if (trackMap.containsKey(customerId)) {
                String trackTime = trackMap.get(customerId).get("TRACK_TIME").toString();
                String visitContent = trackMap.get(customerId).get("VISIT_CONTENT").toString();
                jsonObject.put("TRACK_TIME", DateUtils.parse(trackTime, "MM/dd"));
                jsonObject.put("VISIT_CONTENT", visitContent);
            } else {
                jsonObject.put("TRACK_TIME", "");
                jsonObject.put("VISIT_CONTENT", "");
            }
        }

        return jsonObjectList;
    }

    public Map<String, ExhibitionCusInfo> getExhibitionCustomMap(String exhibitionId) {
        Map<String, ExhibitionCusInfo> map = new HashMap<>();
        exhibitionDao.getExhibitionCustomList(exhibitionId).forEach(jsonObject -> {
            statisticsDeposit(map, jsonObject);
        });

        return map;
    }
    private void statisticsDeposit(Map<String, ExhibitionCusInfo> map, JSONObject jsonObject) {
        ExhibitionCusInfo exhibitionCusInfo;
        String regionId = jsonObject.get("regionId").toString();
        String depositStatus = jsonObject.get("DEPOSIT_STATUS").toString();
        Integer participationNum = Integer.parseInt(jsonObject.get("PARTICIPATION_NUM").toString() == "" ? "0" : jsonObject.get("PARTICIPATION_NUM").toString());
        if (map.containsKey(regionId))
            exhibitionCusInfo = map.get(regionId);
        else {
            exhibitionCusInfo = new ExhibitionCusInfo();
            exhibitionCusInfo.setRegionId(regionId);
            exhibitionCusInfo.setDeposit1(0);
            exhibitionCusInfo.setDeposit2(0);
            exhibitionCusInfo.setDeposit3(0);
            exhibitionCusInfo.setDeposit4(0);
            exhibitionCusInfo.setParticipationNum(0);
            map.put(regionId, exhibitionCusInfo);
        }
        exhibitionCusInfo.setParticipationNum(exhibitionCusInfo.getParticipationNum() + participationNum);

        if (depositStatus.equals("1"))
            exhibitionCusInfo.setDeposit1(exhibitionCusInfo.getDeposit1() + 1);
        else if (depositStatus.equals("2"))
            exhibitionCusInfo.setDeposit2(exhibitionCusInfo.getDeposit2() + 1);
        else if (depositStatus.equals("3"))
            exhibitionCusInfo.setDeposit3(exhibitionCusInfo.getDeposit3() + 1);
        else if (depositStatus.equals("4"))
            exhibitionCusInfo.setDeposit4(exhibitionCusInfo.getDeposit4() + 1);
    }

    public String getYesterday() {
        Date dayCurrent = exhibitionReportDao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dataTime = sdf.format(dayCurrent);
        return dataTime;
    }
}
